package com.wuliu.controller;

import com.wuliu.common.annotation.RepeatSubmit;
import com.wuliu.common.core.domain.ApiResult;
import com.wuliu.common.core.domain.PageResult;
import com.wuliu.common.utils.KuaidiHelpUtil;
import com.wuliu.common.utils.StringUtils;
import com.wuliu.system.domain.dto.applet.BasCustAddressDto;
import com.wuliu.system.domain.dto.applet.QryCustAddressDto;
import com.wuliu.system.service.applet.IBasCustAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wuliu.common.annotation.Log;
import com.wuliu.common.core.controller.BaseController;
import com.wuliu.common.core.domain.AjaxResult;
import com.wuliu.common.enums.BusinessType;
import com.wuliu.system.domain.BasCustAddress;

import java.util.Map;

/**
 * 小程序用户地址库Controller
 *
 * @author realue
 * @date 2023-11-01
 */
@Api(tags = {"地址库控制器"})
@RestController
@RequestMapping("/applet/address")
public class BasCustAddressController extends BaseController {
    @Resource
    private IBasCustAddressService basCustAddressService;

    /**
     * 查询小程序用户地址库列表
     */
    @ApiOperation(value = "查询地址")
    @GetMapping("/list")
    public ApiResult<PageResult<BasCustAddress>> list(QryCustAddressDto dto) {
        return ApiResult.success(basCustAddressService.selectList(dto));
    }

    /**
     * 获取小程序用户地址库详细信息
     */
    @ApiOperation(value = "地址详细")
    @GetMapping(value = "/{id}")
    public ApiResult<BasCustAddress> getInfo(@PathVariable("id") String id) {
        return ApiResult.success(basCustAddressService.selectBasCustAddressById(id));
    }

    /**
     * 新增小程序用户地址库
     */
    @RepeatSubmit
    @ApiOperation(value = "新增地址")
    @PostMapping
    public ApiResult<Integer> add(@RequestBody @Validated BasCustAddressDto dto) {
        return ApiResult.success(basCustAddressService.insertBasCustAddress(dto));
    }

    /**
     * 修改小程序用户地址库
     */
    @ApiOperation(value = "编辑地址")
    @PutMapping
    public ApiResult<Integer> edit(@RequestBody @Validated BasCustAddressDto dto) {
        return ApiResult.success(basCustAddressService.updateBasCustAddress(dto));
    }

    /**
     * 删除小程序用户地址库
     */
    @ApiOperation(value = "删除地址")
    @DeleteMapping("/{ids}")
    public ApiResult<Integer> remove(@PathVariable String[] ids) {
        return ApiResult.success(basCustAddressService.deleteBasCustAddressByIds(ids));
    }

    @GetMapping("/resolveAddress")
    @ApiOperation(value = "解析地址", notes = "")
    @ApiImplicitParam(name = "address", value = "地址", dataType = "String", paramType = "query")
    public ApiResult<Map<String, Object>> resolveAddress(String address) throws Exception {
        if (StringUtils.isEmpty(address)) {
            return ApiResult.error("地址不能为空");
        }
        return ApiResult.success(KuaidiHelpUtil.geocodingAddress(address));
    }
}
